import Vue from 'vue'
import Router from 'vue-router'
import {Message} from "element-ui";
Vue.prototype.$message=Message;
Vue.use(Router)

const routes=[
  {
    path: '',
    redirect:'/login'
  },
  {
    path:'/login',
    component:()=>import('../components/Login')
  },
  {
    path: '/home',
    redirect: '/welcome',
    component:()=>import('../components/Home'),
    children:[
      {
        path:'/welcome',
        component:()=>import('../components/welcome')
      },
      {
        path: '/users',
        component:()=>import('../components/user/users')
      },
      {
        path: '/rights',
        component:()=>import('../components/power/rights')
      },
      {
        path: '/roles',
        component:()=>import('../components/power/roles')
      },
      {
        path: '/categories',
        component:()=>import('../components/goods/Cate')
      },{
        path: '/params',
        component:()=>import('../components/goods/params')
      },{
        path: '/goods',
        component:()=>import('../components/goods/list')
      },
      {
        path: '/goods/add',
        component:()=>import('../components/goods/add')
      },
      {
        path: '/edit/:id',
        component:()=>import('../components/goods/edit')
      },
      {
        path: '/orders',
        component:()=>import('../components/order/order')
      },
      {
        path: '/reports',
        component:()=>import('../components/report/report')
      }
    ]
  }
]

const router=new Router({
  routes,
  mode:'history',
})
router.beforeEach((to,from,next)=>{
  // 如果用户访问登录页面，直接访问
  if(to.path==='/login'){
    return next();
  }
  // 如果用户访问主页但是没有 token 权限， 强制跳到 登录页面
  const tokenStr=window.sessionStorage.getItem("token");
  if(!tokenStr){
    // Vue.$message.info('请先登录');
    return next('/login')
  }
  next();
})
export default router;
